package com.vkdroid.xmpp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.vkdroid.R;

/**
 * Created by Vladimir Grachev.
 * Date: Aug 29, 2010
 * Time: 1:20:54 PM
 */

//TODO: this is temporary activity

public class XmppConnectActivity extends Activity {

    private final static String TAG = "[vkdroid:XmppConnectActivity]";

    private EditText mUsernameView;
    private EditText mPasswordView;
    private EditText mRecipientView;
    private EditText mMessageView;
    private TextView mIncomingMessagesView;
    private Button mConnectButton;
    private Button mDisconnectButton;
    private Button mSendButton;

    private final Handler mHandler = new Handler();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO: this is temporary view
        setContentView(R.layout.xmpp_temp_activity);

        mUsernameView = (EditText)findViewById(R.id.username);
        mPasswordView = (EditText)findViewById(R.id.password);
        mRecipientView = (EditText)findViewById(R.id.recipient);
        mMessageView = (EditText)findViewById(R.id.message);
        mIncomingMessagesView = (TextView)findViewById(R.id.received);
        mConnectButton = (Button)findViewById(R.id.connect);
        mDisconnectButton = (Button)findViewById(R.id.disconnect);
        mSendButton = (Button)findViewById(R.id.send);

        onDisconnect();

    }

    @Override
    protected void onResume() {
        super.onResume();
        XmppUtils.bindToService(this, new ServiceCallback());
    }

    @Override
    protected void onPause() {
        XmppUtils.unbindFromService(this);
        super.onPause();
    }

    public void handleConnect(View view) {
        try {
            XmppUtils.getXmppService().connect(mUsernameView.getText().toString(), mPasswordView.getText().toString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void handleDisconnect(View view) {
        try {
            XmppUtils.getXmppService().disconnect();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    public void handleSend(View view) {
        try {
            XmppUtils.getXmppService().sendMessage(mRecipientView.getText().toString(), mMessageView.getText().toString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    public void onConnect() {
        mUsernameView.setEnabled(false);
        mPasswordView.setEnabled(false);
        mRecipientView.setEnabled(true);
        mMessageView.setEnabled(true);
        mIncomingMessagesView.setEnabled(true);
        mConnectButton.setEnabled(false);
        mDisconnectButton.setEnabled(true);
        mSendButton.setEnabled(true);
    }

    public void onDisconnect() {
        mUsernameView.setEnabled(true);
        mPasswordView.setEnabled(true);
        mRecipientView.setEnabled(false);
        mMessageView.setEnabled(false);
        mIncomingMessagesView.setEnabled(false);
        mConnectButton.setEnabled(true);
        mDisconnectButton.setEnabled(false);
        mSendButton.setEnabled(false);
    }

    public void onMessageReceived(String from, String message) {
        mIncomingMessagesView.append(from + ":" + message + "\n");    
    }

    public class ServiceCallback extends IXmppServiceCallback.Stub {

        public void onServiceReady() throws RemoteException {
        }

        public void onConnect() throws RemoteException {
            mHandler.post(new Runnable() {
                public void run() {
                    XmppConnectActivity.this.onConnect();
                }
            });
        }

        public void onDisconnect() throws RemoteException {
            mHandler.post(new Runnable() {
                public void run() {
                    XmppConnectActivity.this.onDisconnect();
                }
            });
        }

        public void onMessageReceived(final String from, final String message) throws RemoteException {
            Log.d(TAG, "onMessageReceived: " + from + "/" + message);
            mHandler.post(new Runnable() {
                public void run() {
                    XmppConnectActivity.this.onMessageReceived(from, message);
                }
            });
        }
    }

}
